/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.enterprise.web;

import com.assg2.dao.DaoGenre;
import com.assg2.dao.DaoMovie;
import com.movie.Genre;
import com.movie.Movie;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Kevin
 */
public class SearchByGenreCommand implements Command {
    public SearchByGenreCommand () {
    }
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    response.setContentType("text/html");// from response, set content type
        PrintWriter out = response.getWriter();// from response, get output writer
        
        HttpSession session = request.getSession(true);
        List movieList = new ArrayList();
            Movie movie = null;
            Genre genre = null;
            DaoGenre dg = new DaoGenre();
            DaoMovie dm = new DaoMovie();
            movieList = dm.listall();
            int fromyear = 0;
            int toyear = 0;
            genre = (Genre)dg.findById(Integer.parseInt(request.getParameter("genre"))); 
            if(request.getParameter("fromyear")!=""){
             fromyear = (Integer.parseInt(request.getParameter("fromyear"))-1900);
            }else{ fromyear = 50;}
            if(request.getParameter("toyear")!=""){
                 toyear = (Integer.parseInt(request.getParameter("toyear"))-1900);
            }else{ toyear = 149;}
            
            
            Set movieSet = genre.getMovies();
            List<Movie> resultlList = new ArrayList<Movie>();
            Iterator it = movieSet.iterator();
            while(it.hasNext()){
                movie = (Movie)it.next();
                int year = movie.getRelaseDate().getYear();
                if((fromyear<=year)&&(year<=toyear)){
                    resultlList.add(movie);
                
                }                
            }
            request.setAttribute("resultbygenre", resultlList);
            return "/resultByGenre.jsp";
            
        
}
}